<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		ScriptPlanet.eu															/
/	Site:			http://www.scriptplanet.eu												/
/	Email:			support@scriptplanet.eu													/
/	Support:		http://www.scriptplanet.eu/support										/
/	Copyright:		2011 (c) ScriptPlanet.eu												/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0001																	/
/																							/
/========================================== JooSam ========================================*/
// Joomla Security
defined('_JEXEC') or die;

class jsamEvents extends jsamMain{

	public function updateEvent(){

		$conf = $this->settings();

		// TimeZones
		$jsamTz = $conf['samsrvtzone'];
		$currTz = date_default_timezone_get();
		$result = '';
		if($jsamTz != ''){
			
			// Start code
			if($currTz != $jsamTz){
				date_default_timezone_set($jsamTz);
			}

			$date = date('Y-m-d H:i:s');
			$db1 = JFactory::getDBO();
			$query1 = "	SELECT *
						FROM #__joosam_events
							WHERE spstart < '".$date."'
							AND spends < '".$date."'
							AND isdef = 'false'
							AND (sprepeat = 'daily' OR sprepeat = 'weekly')
							ORDER BY id";
			$db1->setQuery($query1);
			$row1 = $db1->loadAssocList();

			if($row1){
				foreach($row1 as $event){
					$id = $event['id'];
					$start = $event['spstart'];
					$end = $event['spends'];
					$repeat = $event['sprepeat'];

					$strStart = strtotime($start);
					$strEnd = strtotime($end);
					
					if($repeat == 'daily'){
						
						$new['daily']['start'] = mktime(date("H", $strStart), date("i", $strStart), date("s", $strStart), date("m", $strStart), date("d", $strStart), date("Y", $strStart));
						$dayC = 0;
						while($new['daily']['start'] < strtotime($date)){
							$dayC = $dayC+1;

							$new['daily']['start'] = mktime(date("H", $strStart), date("i", $strStart), date("s", $strStart), date("m", $strStart), date("d", $strStart)+$dayC, date("Y", $strStart));
							$new['daily']['end'] = mktime(date("H", $strEnd), date("i", $strEnd), date("s", $strEnd), date("m", $strEnd), date("d", $strEnd)+$dayC, date("Y", $strEnd));
						}

						$new['daily']['start'] = date('Y-m-d H:i:s', $new['daily']['start']);
						$new['daily']['end'] = date('Y-m-d H:i:s', $new['daily']['end']);

						$result['id'] = $id;
						$result['type'] = $repeat;
						$result['start_old'] = $start;
						$result['start_new'] = $new['daily']['start'];
						$result['end_old'] = $end;
						$result['end_new'] = $new['daily']['end'];
						$result['counter'] = $dayC.' D';
						$result['update'] = $this->updEvtSql($id, $new['daily']['start'], $new['daily']['end']);

					}

					else if($repeat == 'weekly'){
						
						$new['weekly']['start'] = mktime(date("H", $strStart), date("i", $strStart), date("s", $strStart), date("m", $strStart), date("d", $strStart), date("Y", $strStart));
						$dayW = 0;
						while($new['weekly']['start'] < strtotime($date)){
							$dayW = $dayW+7;

							$new['weekly']['start'] = mktime(date("H", $strStart), date("i", $strStart), date("s", $strStart), date("m", $strStart), date("d", $strStart)+$dayW, date("Y", $strStart));
							$new['weekly']['end'] = mktime(date("H", $strEnd), date("i", $strEnd), date("s", $strEnd), date("m", $strEnd), date("d", $strEnd)+$dayW, date("Y", $strEnd));
						}

						$new['weekly']['start'] = date('Y-m-d H:i:s', $new['weekly']['start']);
						$new['weekly']['end'] = date('Y-m-d H:i:s', $new['weekly']['end']);
						
						$dayW = $dayW/7;
						$result['id'] = $id;
						$result['type'] = $repeat;
						$result['start_old'] = $start;
						$result['start_new'] = $new['weekly']['start'];
						$result['end_old'] = $end;
						$result['end_new'] = $new['weekly']['end'];
						$result['counter'] = $dayW.' W';
						$result['update'] = $this->updEvtSql($id, $new['weekly']['start'], $new['weekly']['end']);
					}
					$data[] = $result;
				}
			}
			else{
				$data = 'code100';
			}
		}
		else{
			$data = 'code101';
		}
		return $data;
	}

	protected function updEvtSql($id, $start, $end){

		// new Database
		$db = JFactory::getDBO();
		$query = "	UPDATE ".$db->nameQuote('#__joosam_events')."
						SET ".$db->nameQuote('spstart')." = ".$db->quote($start).",
							".$db->nameQuote('spends')." = ".$db->quote($end)."
						WHERE ".$db->nameQuote('id')." = ".$db->quote($id)."; ";
		$db->setQuery($query);
		$row = $db->query();
		if($row){
			return '1';
		}
		else{
			return '2';
		}
	}

	public function eventMod(){

		$data = $this->events();
		
		$title = $data['spname'];
		$body = $data['spinfo'];

		$output = '<h3 class="jsam_title">'.$title.'</h3><p class="jsam_text">'.$body.'</p>';
		return $output;
	}
}